<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Index as IndexModel;
use app\admin\model\Auth;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $strArr=  IndexModel::setLongEncrypt(Config::get("miyao.gong"),'515');
        $strTest= IndexModel::setDecryptArray(Config::get("miyao.shi"),$strArr);
        dump($strArr);
        dump($strTest);die;
    }

    /**
     * 首页权益专区展示
     */
    public function equity(){
        $user_id = $this->auth->id;
        $userInfo = Auth::userInfo($user_id);
        if($userInfo['vip_id']>0){
            $is_vip =1;
            $operator_vip  = Db::name('operator_vip')->where('id',$userInfo['vip_id'])->where('is_quanyi',1)->field('id,type,exchange_count,rules')->find();
            if($operator_vip){
                $operator_zone = Db::name('operator_zonefl')->where('vip_id',$userInfo['vip_id'])->where('switch',1)->order('weigh ASE')->select();
                foreach ($operator_zone as &$v){
                    if($v['type'] == 2){//判断专区 还是跳转
                        $v['wl_type'] = Db::name('url_wl')->where('url',$v['url'])->value('type');
                        $v['url_id'] = Db::name('url_wl')->where('url',$v['url'])->value('id');
                        $v['is_do'] =1; //不加锁
                    }else{
                        $exchange_count = Db::name('operator_zone')->where('id',$v['zone_id'])->value('exchange_count');
                        $mapdd['from'] = 2;
                        $mapdd['user_id'] = $userInfo['id'];
                        $mapdd['zone_id'] = $v['zone_id'];
                        if($operator_vip['type'] == 1 || $operator_vip['type'] == 2 || $operator_vip['type'] == 3){
                            $firstday = date('Y-m-01', time());
                            $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
                            $mapdd['createtime'] = ['between', "{$firstday} 00:00:00,{$lastday} 23:59:59"];
                            $zone = Db::name('order')->where($mapdd)->where('status', '<>', 3)->count();
                            if ($exchange_count <= $zone) {
                                $v['is_do'] =2; //加锁
                            }else{
                                $v['is_do'] =1; //不加锁
                            }
                        }else if($operator_vip['type'] == 5){
                            $qinghao = Db::name('qinghao')->where('mobile',$userInfo['mobile'])->where('zone_id',$v['zone_id'])->where('is_do',0)->where('end_time','>',time())->find();
                            if ($qinghao) {
                                $v['is_do'] =1; //不加锁
                            }else{
                                $v['is_do'] =2; //加锁
                            }
                        }else{
                            $mapdd['createtime'] = ['between', "{$userInfo['vip_add_time']} 00:00:00,{$userInfo['vip_end_time']} 23:59:59"];
                            $zone = Db::name('order')->where($mapdd)->where('status', '<>', 3)->count();
                            if ($exchange_count*$operator_vip['exchange_count'] <= $zone) {
                                $v['is_do'] =2; //加锁
                            }else{
                                $v['is_do'] =1; //不加锁
                            }
                        }
                    }
                }
            }else{
                $operator_zone = [];
                $operator_vip['id'] =0;
                $operator_vip['rules']='';
            }
        }
        $info['is_vip'] =$is_vip;
        $info['vip_id'] = $operator_vip['id'];
        $info['list'] = $operator_zone;  //专区列表
        $info['rules'] = $operator_vip['rules']; //会员规则
        $user_qy =Db::name('user_qy')->group('end_time')->where('mobile',$userInfo['mobile'])->select();
        foreach($user_qy as &$v){
            $v['vip_name'] = Db::name('operator_vip')->where('id',$v['vip_id'])->value('vip_name');
        }
        $info['qy'] =$user_qy;
        $this->success("信息",$info);
    }


    /**
     * 商品分类
     */
    public function category(){
        $category = input('category_id');
        if(empty($category)){
            $data =  Db::name('category')->where('pid',0)->order('weigh DESC')->where('status',1)->select();
        }else{
            $data = Db::name('category')->where('pid',$category)->order('weigh DESC')->where('status',1)->select();
        }
        $this->success("上传成功",$data);
    }

    /**
     *  搜索
     */
    public function search(){
        $content = input('content');
        $page = input('page',1);
        $page_size = input('page_size',10);
        if($content){
            $map['name'] = ['like','%'.$content.'%'];
            $list = Db::name('ctrip')
                ->where('cansale','true')
//               ->where('businessstatus',1)
                ->where('ctripprice','>',0)
                ->where($map)
                ->page($page,$page_size)
                ->field('id,
               scenicspotid as item_id,
               name,
               address,
               ctrip_sales as total_sales,
               price as activity_price_cent,
               url_image as main_picture,
               ctripprice,latitude,longitude,type'
                )
                ->select();
            if(count($list)>0){
                foreach($list as $k=>$v){
                    $list[$k]['title'] =$v['name'];
                    $list[$k]['activity_price_cent'] = (float)$v['activity_price_cent'];
                    $list[$k]['original_price_cent'] = round($v['ctripprice']*0.2+$v['ctripprice'],2);
                }
                $count = Db::name("ctrip")->where('cansale','true')->where('ctripprice','>',0)->where($map)->count();
                $data = [
                    'list'=>$list,
                    'count'=>$count
                ];
                $this->success("获取成功",$data);
            }
            //阿里本地生活推荐
            $data = IndexModel::aliList($city='',$yiji_id='',$page,$page_size,$erji_id='',$content);
            $this->success("获取成功",$data);
        }
        $data = [
            'list'=>0,
            'count'=>0
        ];
        $this->success("获取成功",$data);
    }


    /**
     * 每日精选好货
     */
    public function goodsList(){
        $type = input('type');      //类型 1景点 2商品 3电影票
        $yiji_id = input('yiji_id'); //一级分类
        $erji_id = input('erji_id'); //二级分类
        $city= input('city');        //城市
        $page = input('page',1);
        $page_size = input('page_size',10);
        $data = [];
        if($type==1){
            $map = [];
            if($erji_id){
                $map['star'] = ['in',Db::name('ali_category')->where('erji',$erji_id)->column('star')];
            }
            //这里写景点门票的代码
            $list = Db::name('ctrip')
                ->where('cityname',$city)
                ->where('cansale','true')
//                ->where('businessstatus',1)
                ->where('ctripprice','>',0)
                ->where($map)
                ->page($page,$page_size)
                ->field('id,
               scenicspotid as item_id,
               name as title,
               address,
               ctrip_sales as total_sales,
               price as activity_price_cent,
               url_image as main_picture,
               ctripprice,latitude,longitude,type'
                )
                ->select();
            if(!empty($list)){
                foreach($list as $k=>$v){
                    $list[$k]['distance'] = 0;
                    $list[$k]['activity_price_cent'] = (float)$v['activity_price_cent'];
                    $list[$k]['original_price_cent'] = round($v['ctripprice']*0.2+$v['ctripprice'],2);
                }
            }
            $count = Db::name("ctrip")->where('cityname',$city)->where('cansale','true')->where('ctripprice','>',0) ->count();
            $data = [
                'list'=>$list,
                'count'=>$count
            ];
        }else if($type == 2 ){
            //阿里本地生活推荐
            $data = IndexModel::aliList($city,$yiji_id,$page,$page_size,$erji_id);
        }
        $this->success("获取成功",$data);
    }

    /**
     * 获取城市列表
     */
    public function getSearchCitys(){
        $data = Db::name("ali_city")->column('city_name');
        if(!empty($data)){
            $list = [];
            foreach($data as $k=>$v){
                $list[$k]['name'] = _getFirstCharter(trim($v));
                $list[$k]['title'] = $v;
                if(empty($list[$k]['name'])){
                    unset($list[$k]);
                }
            }
        }
        $lists = groupByInitials($list);
        $this->success("获取成功",[
            'hot_city'=>[
                '贵阳', '六盘水', '遵义', '安顺', '毕节', '同仁', '黔西南布依族苗族自治州', '黔东南苗族侗族自治州', '黔南布依族苗族自治州'
            ],
            'citys'=>$lists
        ]);

    }


}
